package com.gree.scmservice.controller.saas.admin;

import com.gree.common.entity.ReturnVo;
import com.gree.common.entity.TablePageData;
import com.gree.scmservice.entity.saas.SaasCommodity;
import com.gree.scmservice.entity.saas.vo.SaasCommodityVo;
import com.gree.scmservice.service.saas.SaasCommdityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

/**
 * 包路径: com.gree.scmservice.controller.saas
 * 项目名: scm-api
 * 创建时间: 2021-07-12 15:52
 * 描述: TODO
 **/
@Api(tags = "SAAS系统商品(管理端)")
@RestController
@RequestMapping("/saas/admin/commdity")
public class AdminCommdityController {

    @Autowired
    private SaasCommdityService saasCommodityService;

    /**
     * 查询SAAS系统商品列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询SAAS系统商品列表")
    @ApiImplicitParam(name = "saasCommodity", value = "SAAS系统商品", dataType = "SaasCommodity")
    public ReturnVo<TablePageData<SaasCommodityVo>> list(SaasCommodity saasCommodity)
    {
        TablePageData<SaasCommodity> list = saasCommodityService.findAllPageCommodity(saasCommodity);
        return ReturnVo.success(list);
    }

    /**
     * 获取SAAS系统商品详细信息
     */
    @GetMapping(value = "/{groupId}")
    @ApiOperation(value = "获取SAAS系统商品详细信息")
    @ApiImplicitParam(name = "groupId", value = "商品ID", dataType = "String")
    public ReturnVo getInfo(@PathVariable("groupId") String groupId)
    {
        return ReturnVo.success(saasCommodityService.getByIdCommodity(groupId));
    }

    /**
     * 新增SAAS系统商品
     */
    @PostMapping
    @ApiOperation(value = "新增SAAS系统商品")
    @ApiImplicitParam(name = "saasCommodityVo", value = "SAAS系统商品", dataType = "SaasCommodityVo")
    public ReturnVo add(@RequestBody SaasCommodityVo saasCommodityVo)
    {
        return ReturnVo.toAjax(saasCommodityService.saveCommodity(saasCommodityVo));
    }

    /**
     * 修改SAAS系统商品
     */
    @PutMapping
    @ApiOperation(value = "修改SAAS系统商品")
    @ApiImplicitParam(name = "saasCommodityVo", value = "SAAS系统商品", dataType = "SaasCommodityVo")
    public ReturnVo edit(@RequestBody SaasCommodityVo saasCommodityVo)
    {
        return ReturnVo.toAjax(saasCommodityService.updateCommodity(saasCommodityVo));
    }

    /**
     * 删除SAAS系统商品
     */
    @DeleteMapping("/{groupIds}")
    @ApiOperation(value = "删除SAAS系统商品")
    @ApiImplicitParam(name = "groupIds", value = "商品ID", dataType = "String[]")
    public ReturnVo remove(@PathVariable String[] groupIds)
    {
        return ReturnVo.toAjax(saasCommodityService.deleteCommodity(groupIds));
    }

    /**
     * 上传文件
     */
    @PostMapping("/upload")
    @ApiOperation(value = "上传SAAS系统商品图片")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "file", value = "文件", dataType = "MultipartFile"),
            @ApiImplicitParam(name = "groupId", value = "商品ID", dataType = "String")
    })
    public ReturnVo upload(MultipartFile file, String groupId) {
        return saasCommodityService.uploadIcon(file, groupId);
    }

    /**
     * 下载文件
     */
    @GetMapping("/download")
    @ApiOperation(value = "下载SAAS系统商品图片")
    @ApiImplicitParam(name = "path", value = "商品图片", dataType = "String")
    public void download(@RequestParam String path, HttpServletResponse response) {
        saasCommodityService.downloadImage(path, response);
    }
}

